package com.mmlc.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;
import android.widget.TextView;

import com.mmlc.meimei.lichengkongjian.R;


/**
 * Created by Administrator on 2016/9/28.
 */
public class CheckableTextView extends TextView implements Checkable {

    private boolean check;

    public CheckableTextView(Context context) {
        super(context);
        initView();
    }

    public CheckableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public CheckableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                toggle();
            }
        });
    }


    @TargetApi(21)
    public CheckableTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    @Override
    public void setChecked(boolean checked) {
        this.check = checked;
        if (checked) {
            setBackground(getResources().getDrawable(R.drawable.checkable_tv_bg));
            setTextColor(getResources().getColor(R.color.text_red));
        } else {
            setBackground(getResources().getDrawable(R.drawable.price_edit_bg));
            setTextColor(getResources().getColor(R.color.text_grey));
        }
    }

    @Override
    public boolean isChecked() {
        return check;
    }

    @Override
    public void toggle() {
        setChecked(!check);
        if (mOnCheckedChangeListener!=null)
            mOnCheckedChangeListener.onChecked(check);
    }

    private OnCheckedChangeListener mOnCheckedChangeListener;

    public OnCheckedChangeListener getOnCheckedChangeListener() {
        return mOnCheckedChangeListener;
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        mOnCheckedChangeListener = onCheckedChangeListener;
    }

    public boolean isCheck() {
        return check;
    }

    public void setCheck(boolean check) {
        this.check = check;
    }

    public interface OnCheckedChangeListener {
        public void onChecked(boolean checked);
    }

}
